<script setup>
import { h, computed, ref } from 'vue';
import { CaretDownOutlined, CaretRightOutlined, DeleteOutlined } from '@ant-design/icons-vue';
import { selectedLayers, editingLayers } from '../common.js';
import { picState } from '../api/use-task.js';
import { UserOpGte } from '../op-perms';
import { clearList, selectLayer } from '../api/use-layer-common.js';
import LabelLayer from './label-layer.vue';

const open = ref(true);
const CaretIcon = computed(() => () => h(open.value ? CaretDownOutlined : CaretRightOutlined));
</script>

<template>
  <details
    class="label-layers"
    :open="open"
    @toggle="event => open = event.currentTarget.open"
  >
    <summary>
      <caret-icon></caret-icon>
      <span>已标注（{{ picState.layers.length }}）</span>
      <div class="fn">
        <template v-if="UserOpGte('标注')">
          <a-button :disabled="!picState.layers.length" @click.stop="() => clearList('layers')">
            <template #icon><delete-outlined style="color: rgb(200,0,0);" /></template>
          </a-button>
        </template>
      </div>
    </summary>

    <div class="ol-container">
      <ol @click.self="() => editingLayers = []">
        <template v-for="(layer, i) in picState.layers" :key="i">
          <label-layer
            :class="{ selected: selectedLayers.includes(layer) }"
            :style="{ 'border-color': layer.color }"
            v-model:item="picState.layers[i]"
            @click.native.self="event => selectLayer('switch', layer, event.ctrlKey)">
          </label-layer>
        </template>
      </ol>
    </div>
  </details>
</template>

<style>
.label-layers {
  height: auto;
  padding-bottom: 8px;
  border-left: 1px solid rgb(228,228,228);
  background: #fff;
}

.label-layers[open] { flex: 1; }

.label-layers > summary {
  display: flex;
  gap: .5em;
  align-items: center;
  width: 100%;
  height: 40px;
  padding-left: 6px;
  padding-right: 12px;
  border-bottom: 1px solid rgb(228,228,228);
}

.label-layers > summary > .fn {
  display: flex;
  gap: .5em;
  margin-left: auto;
}

.label-layers > summary > .anticon:first-child { font-size: 12px; }

.label-layers > .ol-container {
  position: relative;
  height: calc(100% - 40px);
}

.label-layers > .ol-container > ol {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow-y: auto;
}

.label-layers .ant-btn {
  width: auto;
  height: auto;
  padding-left: 4px;
  padding-right: 4px;
  border: 0;
  background: transparent;
}
.label-layers .ant-btn:focus { color: unset; }
</style>
